﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer.Base;
using SqlCeViewer.CommonMethod;
using System.IO;

namespace SqlCeViewer.UI
{
    public partial class Access2SDF : BaseSubForm
    {
        public Access2SDF()
        {
            InitializeComponent();
        }

        private void Access2SDF_Load(object sender, EventArgs e)
        {

        }

        private void butSelectAccessFile_Click(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            sf.Filter = GlobalInfo.MDB_FILE_FILTER;
            if (sf.ShowDialog() == DialogResult.OK)
            {
                this.buttonEdit1.Text = sf.FileName;
            }
        }

        private void butImport_Click(object sender, EventArgs e)
        {

            string sdfFile = this.buttonEdit2.Text;

            string mdbFile = this.buttonEdit1.Text;

            string pwd = this.textEdit1.Text;


            if (string.IsNullOrEmpty(mdbFile))
            {
                MessageBox.Show("Please select source access file first");
                return;
            }
            if(!File.Exists(mdbFile))
            {
                MessageBox.Show("File not existed,please try again");
                return;
            }
            if (string.IsNullOrEmpty(sdfFile))
            {
                MessageBox.Show("Please select source access file first");
                return;
            }
            //是否导出到的是已经存在的DB
            bool IsImportToExsitedDb=File.Exists(sdfFile);
            if (!IsImportToExsitedDb)
            {
                if (!GlobalInfo.MainEngineer.CreateDataBase(sdfFile))
                {
                    MessageBox.Show("Create sql ce database failure,this action will cancel");
                    return;
                }
            }

            if (CommonUtil.ConvertMdbToSdf(mdbFile, sdfFile, pwd,(bool)this.checkEdit1.Checked))
            {
                MessageBox.Show(String.Format("Sync data from \r\n{0} \r\nto \r\n{1} \r\nSuccessful.", mdbFile, sdfFile));
                Close();
            }
        }

        private void buttonEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            sf.Filter = GlobalInfo.SQLCE_FILE_FILTER;
            if (sf.ShowDialog() == DialogResult.OK)
            {
                this.buttonEdit2.Text = sf.FileName;
            }
        }
    }
}